package com.petShop.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.petShop.dao.CommentDao;
import com.petShop.dao.GoodDao;
import com.petShop.dao.UserDao;
import com.petShop.models.Comment;
import com.petShop.models.Good;

@Controller
@Transactional
@RequestMapping("/petGood")
public class PetGoodsController {
	@Autowired
	GoodDao goodDao;
	@Autowired
	UserDao userDao;
	@Autowired
	CommentDao commentDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getPetGoods(HttpSession session,HttpServletRequest req){
		ModelAndView mav = new ModelAndView("/petGood/petGoods");
		List<Good> petGoods = goodDao.selectPetGoods();
		mav.addObject("petGoods",petGoods);
		//获取目前地址
		String returnUrl = req.getRequestURI();
		session.setAttribute("returnUrl", returnUrl);
		
		return mav;
	}
	
	@RequestMapping(value="choose",method=RequestMethod.GET)
	public ModelAndView choose(@RequestParam String kindsId,HttpSession session,HttpServletRequest req){
		ModelAndView mav = new ModelAndView();
		int id = 0;
		if(kindsId!=null){
			id = Integer.parseInt(kindsId);
			
			//获取目前地址
			String returnUrl = req.getRequestURI();
			session.setAttribute("returnUrl", returnUrl+"?kindsId="+id);
		}
		List<Good> petGoods = goodDao.selectkinds(id);
		
		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	
	@RequestMapping(value="searchgoods",method=RequestMethod.GET)
	public ModelAndView searchgoods(@RequestParam String keyword,@RequestParam int lowPrice,@RequestParam int highPrice){
		ModelAndView mav = new ModelAndView();
		
		Good good = new Good();
		good.setKeyword("%"+keyword+"%");
		
		good.setLowPrice(lowPrice);
		good.setHighPrice(highPrice);

		List<Good> petGoods = goodDao.searchPetGoods(good);
		
		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	@RequestMapping(value="searchBylowPrice",method=RequestMethod.GET)
	public ModelAndView searchBylowPrice(@RequestParam int lowPrice){
		ModelAndView mav = new ModelAndView();
		
		Good good = new Good();
		good.setLowPrice(lowPrice);
		List<Good> petGoods = goodDao.searchPetGoodsBylowPrice(good);
		
		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	@RequestMapping(value="searchByhighPrice",method=RequestMethod.GET)
	public ModelAndView searchByhighPrice(@RequestParam int highPrice){
		ModelAndView mav = new ModelAndView();
		
		Good good = new Good();
		good.setHighPrice(highPrice);
		List<Good> petGoods = goodDao.searchPetGoodsByhighPrice(good);
		
		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	@RequestMapping(value="searchgoodsByName",method=RequestMethod.GET)
	public ModelAndView searchgoodsByName(@RequestParam String keyword){
		ModelAndView mav = new ModelAndView();
		
		Good good = new Good();
		good.setKeyword("%"+keyword+"%");
		List<Good> petGoods = goodDao.searchPetGoodsByName(good);

		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	
	@RequestMapping(value="searchByNameAndLowPrice",method=RequestMethod.GET)
	public ModelAndView searchByNameAndLowPrice(@RequestParam String keyword,@RequestParam int lowPrice){
		ModelAndView mav = new ModelAndView();
		
		Good good = new Good();
		good.setKeyword("%"+keyword+"%");
		good.setLowPrice(lowPrice);
		List<Good> petGoods = goodDao.searchPetGoodsByNameAndLowPrice(good);

		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	
	@RequestMapping(value="searchByNameAndHighPrice",method=RequestMethod.GET)
	public ModelAndView searchByNameAndHighPrice(@RequestParam String keyword,@RequestParam int highPrice){
		ModelAndView mav = new ModelAndView();
		
		Good good = new Good();
		good.setKeyword("%"+keyword+"%");
		good.setHighPrice(highPrice);
		List<Good> petGoods = goodDao.searchPetGoodsByNameAndHighPrice(good);

		mav.addObject("petGoods",petGoods);	
		mav.setViewName("/petGood/petGoods");
		return mav;
	}
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	public ModelAndView getPet(@PathVariable int id,HttpSession session,HttpServletRequest req){
		ModelAndView mav = new ModelAndView("/petGood/petGood");
		//获取目前地址
		String returnUrl = req.getRequestURI();
		session.setAttribute("returnUrl", returnUrl);
		
		
		Good petGood = goodDao.selectGood(id);
		//获取商品Id用于评论
		session.setAttribute("goodId", petGood.getGoodId());
		mav.addObject("petGood",petGood);
		//显示评论
		List<Comment> comments = commentDao.selectComments(id);
		mav.addObject("comments",comments);
		
		return mav;
	}
}
